package control;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

import model.Shape;
import model.Wall;
import view.GamePanel;
import view.ShowPanel;

public class ControlMain extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 356498983721732114L;
	private GamePanel gp;
	private ShowPanel sp;
	private JMenuBar jmb;
	private JMenu jm;
	private JMenuItem jmi;
	private Shape shape;
	private Wall wall;
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				new ControlMain();
			}
		});
	}

	public ControlMain() {
		addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent k) {
				if (k.getKeyCode() == KeyEvent.VK_RIGHT)
					shape.ChangeDirection(Shape.RIGHT);
				else if (k.getKeyCode() == KeyEvent.VK_LEFT)
					shape.ChangeDirection(Shape.LEFT);
				else if (k.getKeyCode() == KeyEvent.VK_DOWN)
					shape.ChangeDirection(Shape.DOWN);
				else if(k.getKeyCode()==KeyEvent.VK_SPACE){
					shape.Rotate();
				}
			}
		});
		wall=new Wall();
		Font font = new Font("华文彩云", Font.LAYOUT_LEFT_TO_RIGHT, 15);
		jmb = new JMenuBar();
		setJMenuBar(jmb);
		setTitle("俄罗斯方块");
		setVisible(true);
		setSize(600, 550);
		setResizable(false);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension framesize = this.getSize();
		int x = (int) screensize.getWidth() / 2 - (int) framesize.getWidth() / 2;
		int y = (int) screensize.getHeight() / 2 - (int) framesize.getHeight() / 2;
		setLocation(x, y);
		setLayout(null);
		shape = new Shape();
		gp = new GamePanel(wall);
		add(gp);
		shape.addObserver(gp);
		sp = new ShowPanel();
		add(sp);
		shape.addObserver(sp);
		shape.Init();
		String[] menu = { "游戏", "难度", "关于" };
		String[][] item = { { "新游戏", "暂停", "继续", "结束" }, { "高级", "中级", "低级" }, { "帮助" } };
		for (int i = 0; i < menu.length; i++) {
			jm = new JMenu(menu[i]);
			for (int j = 0; j < item[i].length; j++) {
				jmi = new JMenuItem(item[i][j]);
				jmi.setFont(font);
				jmi.addActionListener(this);
				jm.add(jmi);
			}
			jm.setFont(font);
			jmb.add(jm);
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

	}
}
